package cn.iocoder.an.module.business.service.partlist;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import cn.iocoder.an.module.business.dal.dataobject.drawconfig.DrawConfigDO;
import cn.iocoder.an.module.business.dal.dataobject.storeinit.StoreInitDO;
import cn.iocoder.an.module.system.dal.dataobject.user.AdminUserDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.micrometer.common.util.StringUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import cn.iocoder.an.module.business.controller.admin.partlist.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.partlist.PartListDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.business.dal.mysql.partlist.PartListMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.*;

/**
 * 零件号 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class PartListServiceImpl implements PartListService {

    @Resource
    private PartListMapper partListMapper;

    @Override
    public Long createPartList(PartListSaveReqVO createReqVO) {
        // 插入
        PartListDO partList = BeanUtils.toBean(createReqVO, PartListDO.class);
        partListMapper.insert(partList);

        // 返回
        return partList.getId();
    }

    @Override
    public void updatePartList(PartListSaveReqVO updateReqVO) {
        // 校验存在
        validatePartListExists(updateReqVO.getId());
        // 更新
        PartListDO updateObj = BeanUtils.toBean(updateReqVO, PartListDO.class);
        partListMapper.updateById(updateObj);
    }

    @Override
    public void deletePartList(Long id) {
        // 校验存在
        validatePartListExists(id);
        // 删除
        partListMapper.deleteById(id);
    }

    @Override
        public void deletePartListListByIds(List<Long> ids) {
        // 删除
        partListMapper.deleteByIds(ids);
        }


    private void validatePartListExists(Long id) {
        if (partListMapper.selectById(id) == null) {
            throw exception(PART_LIST_NOT_EXISTS);
        }
    }

    @Override
    public PartListDO getPartList(Long id) {
        return partListMapper.selectById(id);
    }

    @Override
    public PageResult<PartListDO> getPartListPage(PartListPageReqVO pageReqVO) {
        return partListMapper.selectPage(pageReqVO);
    }

    @Override
    public ImportRespVO importList(List<PartListImportVo> list, boolean isUpdateSupport) {
        // 1.1 参数校验
        if (CollUtil.isEmpty(list)) {
            throw exception(IMPORT_EQUIPMENT_MANAGE_NOT_EXISTS);
        }

        //2.记录成功或失败记录
        ImportRespVO respVO = ImportRespVO.builder().insertNameList(new ArrayList<>())
                .updateNameList(new ArrayList<>()).failureMap(new LinkedHashMap<>()).build();

        AtomicInteger count = new AtomicInteger(1);
        List<PartListDO> rusultList = new ArrayList<>();

        list.forEach(drawConfig -> {
            count.incrementAndGet();//记录行号


            if (StringUtils.isEmpty(drawConfig.getPartNumber())) {
                throw exception("第" + count + "行数据" + "零件号不能为空!");
            }

            if (StringUtils.isEmpty(drawConfig.getSupplyCode())) {
                throw exception("第" + count + "行数据" + "零件号不能为空!");
            }


            rusultList.add(BeanUtils.toBean(drawConfig, PartListDO.class));
            respVO.getInsertNameList().add(drawConfig.getPartNumber());
        });
        if (respVO.getFailureMap().size() > 0) {
            respVO.getInsertNameList().clear();
            return respVO;
        }
        //一次性保存
        partListMapper.insertBatch(rusultList);

        return respVO;
    }

}